package com.ly.web.controller.erp;

import com.alibaba.excel.EasyExcel;
import com.ly.common.annotation.Log;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.common.utils.erp.EasyExcelListener;
import com.ly.heart.domain.KingBaseTaxTypeEntity;
import com.ly.heart.domain.dto.KingBaseTaxTypeExcelDto;
import com.ly.heart.service.IKingBaseTaxTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * <p>
 * 基础数据-税收分类编码-物料中使用 前端控制器
 * </p>
 *
 * @author WuZH
 * @since 2024-08-06
 */
@Api ( value = "kingBaseTaxType", tags = "金蝶-税收分类编码" )
@RestController
@RequestMapping ( "/heart/kingBaseTaxType" )
@Slf4j
public class KingBaseTaxTypeController {

    @Resource
    private IKingBaseTaxTypeService iKingBaseTaxTypeService;
    @Resource
    private EasyExcelListener easyExcelListener;
    @Value ( "${downPath}" )
    private String downPath;

    @GetMapping ( "list" )
    @ApiOperation ( "基础数据-税收分类编码列表" )
    public AjaxResult queryList ( KingBaseTaxTypeEntity kingBaseTaxTypeEntity ) {
        AjaxResult ajaxResult = iKingBaseTaxTypeService.queryList ( kingBaseTaxTypeEntity );
        return ajaxResult;
    }

    @Log ( title = "金蝶-税收分类编码", businessType = BusinessType.INSERT )
    @PostMapping
    @ApiOperation ( "新增基础数据-税收分类编码" )
    public AjaxResult saveSingle ( @RequestBody KingBaseTaxTypeEntity kingBaseTaxTypeEntity ) {
        AjaxResult ajaxResult = iKingBaseTaxTypeService.saveSingle ( kingBaseTaxTypeEntity );
        return ajaxResult;
    }


    /**
     * 下载Excel模板
     * @param response
     * @throws UnsupportedEncodingException
     */
    @GetMapping ( "download" )
    @ApiOperation ( "新增基础数据-下载Excel模板" )
    public void download ( HttpServletResponse response ) throws UnsupportedEncodingException {
        File file = new File ( downPath + "/temp.xlsx" );
        // 设置响应头信息
        String filename = "temp.xlsx";
        // 设置response的Header
        response.setCharacterEncoding ( "UTF-8" );
        // 指定下载文件名(attachment-以下载方式保存到本地，inline-在线预览)
        response.setHeader ( "Content-Disposition", "attachment; filename=" + URLEncoder.encode ( filename, "UTF-8" ) );
        // 告知浏览器文件的大小
        response.addHeader ( "Content-Length", "" + file.length () );
        // 内容类型为通用类型，表示二进制数据流
        response.setContentType ( "application/octet-stream" );
        try ( InputStream is = new FileInputStream ( file );
              BufferedInputStream bis = new BufferedInputStream ( is );
              BufferedOutputStream bos = new BufferedOutputStream ( response.getOutputStream () ) ) {
            byte[] buff = new byte[2048];
            int len;
            while ( ( len = bis.read ( buff ) ) != - 1 ) {
                bos.write ( buff, 0, len );
            }
            bos.flush ();
        } catch ( IOException e ) {
            e.printStackTrace ();
        }
    }


    /**
     * 上传Excel并解析入库
     * @param file
     * @return
     */
    @PostMapping ( "excel" )
    @ApiOperation ( "新增基础数据-税收分类编码导入Excel" )
    public AjaxResult importExcel ( @RequestParam ( "file" ) MultipartFile file ) {
        if ( file.isEmpty () ) {
            return AjaxResult.error ( "解析文件不能为空！" );
        }
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream ();
        } catch ( IOException e ) {
            e.printStackTrace ();
            return AjaxResult.error ( "文件已损坏，无法解析！" );
        }
        EasyExcel.read ( inputStream, KingBaseTaxTypeExcelDto.class, easyExcelListener ).sheet ().doRead ();
        AjaxResult ajaxResult = null;
        if ( easyExcelListener.isCorrectFormat () ) {
            //入库
            ajaxResult = iKingBaseTaxTypeService.saveBatchList ( easyExcelListener.getList () );
        }
        return ajaxResult;
    }

    @Log (title = "删除税收分类编码", businessType = BusinessType.DELETE)
    @DeleteMapping ("/{ids}")
    @ApiOperation ("删除金蝶")
    public AjaxResult remove (@PathVariable String[] ids) {
        return iKingBaseTaxTypeService.deleteByIds ( ids );
    }

    @Log (title = "禁用金蝶-税收分类编码", businessType = BusinessType.DELETE)
    @RequestMapping ("disabled")
    @ApiOperation ("禁用金蝶-税收分类编码")
    public AjaxResult disabled (String id) {
        return iKingBaseTaxTypeService.disabled ( id );
    }

}
